package com.photoeditor.demo.model.bean;

import com.photoeditor.demo.db.DataManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * Created by @author winn on 2018/10/30.
 */

public class EthnicityDataManager {

    private static LinkedHashMap<String, String> sWhiteEthnicityMap = new LinkedHashMap<>();
    private static LinkedHashMap<String, String> sAsianEthnicityMap = new LinkedHashMap<>();
    private static LinkedHashMap<String, String> sHispanicEthnicityMap = new LinkedHashMap<>();

    static {
        sWhiteEthnicityMap.put("Caucasian", "0");
        sWhiteEthnicityMap.put("Middle Eastern/North African", "1");

        sAsianEthnicityMap.put("East Asian", "2");
        sAsianEthnicityMap.put("Southeast Asian", "3");
        sAsianEthnicityMap.put("South Asian", "4");

        sHispanicEthnicityMap.put("Hispanic", "5");
        sHispanicEthnicityMap.put("Latino", "6");
    }

    private static EthnicityDataManager mInstance = null;

    public static EthnicityDataManager getInstance() {
        if (mInstance == null) {
            synchronized (DataManager.class) {
                if (mInstance == null) {
                    mInstance = new EthnicityDataManager();
                }
            }
        }
        return mInstance;
    }

    private EthnicityDataManager() {
    }

    public List<EthnicityBean> getEthnicity(List<EthnicityBean> orgData) {
        ArrayList<EthnicityBean> ret = new ArrayList<>();
        EthnicityBean white = new EthnicityBean(EthnicityBean.ETHNICITY_TYPE_PARENT, "White", "0");
        ret.add(white);
        List<EthnicityBean> child = getChild(orgData, sWhiteEthnicityMap, white.getItemName());
        if (child != null && child.size() > 0) {
            white.setExpand(true);
            ret.addAll(child);
        }

        EthnicityBean asian = new EthnicityBean(EthnicityBean.ETHNICITY_TYPE_PARENT, "Asian", "0");
        ret.add(asian);
        child = getChild(orgData, sAsianEthnicityMap, asian.getItemName());
        if (child != null && child.size() > 0) {
            asian.setExpand(true);
            ret.addAll(child);
        }

        EthnicityBean hispanic = new EthnicityBean(EthnicityBean.ETHNICITY_TYPE_PARENT, "Hispanic/Latino", "0");
        ret.add(hispanic);
        child = getChild(orgData, sHispanicEthnicityMap, hispanic.getItemName());
        if (child != null && child.size() > 0) {
            hispanic.setExpand(true);
            ret.addAll(child);
        }

        EthnicityBean black = new EthnicityBean(EthnicityBean.ETHNICITY_TYPE_PARENT, "Black", "7");
        ret.add(black);
        black.setHasChild(false);

        EthnicityBean other = new EthnicityBean(EthnicityBean.ETHNICITY_TYPE_PARENT, "Other", "0");
        other.setSelAble(false);
        ret.add(other);
        other.setHasChild(false);
        return ret;
    }

    private List<EthnicityBean> getChild(List<EthnicityBean> orgData, HashMap<String, String> child, String parentName) {
        if (orgData == null || orgData.size() == 0) {
            return null;
        }
        ArrayList<EthnicityBean> ret = new ArrayList<>();
        boolean expand = false;
        for (EthnicityBean item : orgData) {
            if (item.getItemType() == EthnicityBean.ETHNICITY_TYPE_PARENT
                    && item.getItemName().endsWith(parentName)) {
                expand = item.isExpand();
                break;
            }
        }
        if (expand) {
            for (String key : child.keySet()) {
                EthnicityBean white = new EthnicityBean(EthnicityBean.ETHNICITY_TYPE_CHILD, key, child.get(key));
                ret.add(white);
            }
        }
        return ret;
    }
}
